/*******LJL 20150820*******/
var CurrModelObj = [];
var CurrAllModelObj = [];
var CurElemTypeObjTemp = [];
var CurElemTypeObj = [];
///////////////////////
var confTypeMap = new Map();
var elemTypeMap = new Map();
var cmdTypeMap = new Map();
var xotree = new XML.ObjTree();
var dumper = new JKL.Dumper();
var xmlText;
var tree;
var paramTypejsonData;
/////////////////////////
var ctrlTypeMap = new Map();
var interfaceTypeMap = new Map();
var LONinterfaceTypeMap = new Map();
var l880interfaceTypeMap = new Map();
var NICODMX512interfaceTypeMap = new Map();
var areaTypeMap = new Map();
var ModelTypeMap = new Map();
var sceneTypeMap = new Map();
var floorTypeMap = new Map();
var cmdOemIdModelMap = new Map();
///////////////////////

/****获取sysParam.xml*****/
function GetparamTypexmlTjson() {
	$.ajax({
		url : '../orgMgr/devCfg/loadCfgTypes',
		type : 'get',
		dataType : 'text',
		timeout : 100000,
		cache : false,
		async: false,
		error : function () {
			alert("获取参数超时");
		},
		success : function (dataxml) {
			tree = xotree.parseXML(dataxml);
			paramTypejsonData = dumper.dump(tree);
			paramTypejsonData = $.parseJSON(paramTypejsonData);
			CreatparamTypMap(paramTypejsonData);
			FillSelect(elemTypeMap, 'SelectElemType');
			//GetAppCfgxmlTjson();
		}
	})
}

/****sysParam.xml解析****/
function CreatparamTypMap(json) {
	confTypeMap.put('01', '协议型');
	confTypeMap.put('02', '配置型');
	$.each(json.paramTypes.paramType, function (index, item) {
		switch (item.type) {
		case 'elemType':
			$.each(json.paramTypes.paramType[index].param, function (Subindex, Subitem) {
				elemTypeMap.put(Subitem.code, Subitem.name);
			});
			break;
		case 'cmdType':
			InitMcmdArry = json.paramTypes.paramType[index];
			$.each(json.paramTypes.paramType[index].param, function (Subindex, Subitem) {
				cmdTypeMap.put(Subitem.ctype, Subitem.name);
			});
			break;
		default:
		}
	})
}

/****获取命令模板表格数据****/
function GetCMDTableResult(gridId) {
	var data = [];
	data = $.fn.tables.GetCMDTableResult(gridId);
	return data;
}

/****获取命令模板表格数据****/
$.fn.tables.GetCMDTableResult = function (gridId) {
	var ir = '0';
	var resultArray = new Array();
	$("#" + gridId).find("tr").slice(1).each(function () {
		var isRepeat = false;
		var id,
		oid,
		name,
		dtype,
		val;
		var isNull = "";
		$(this).find("td").each(function () {
			var $child = $(this).children();
			if ($child.attr("name") == 'id')
				id = $child.val();
			if ($child.attr("name") == 'name')
				name = $child.val();
			if ($child.attr("name") == 'oid')
				oid = $child.val();
			if ($child.attr("name") == 'dtype')
				dtype = $child.val();
			if ($child.attr("name") == 'val')
				val = $child.val();
		});
		isNull += id + name + oid + dtype + val;
		if (isNull == "")
			console.log("CMDTR IS null");
		else
			resultArray.push({
				"id" : id,
				"oid" : oid,
				"val" : val,
				"dtype" : dtype,
				"name" : name
			});
	});
	$.unique(resultArray);
	if (resultArray.length == 0)
		return ir;
	else
		return resultArray;
};

/****CurrAllModelObj转化成xml并上传****/
function UpDataModelXml() {
	var UpdatadevXmlStatus = false;
	var xotree = new XML.ObjTree();
	var dataxml = formatXml(xotree.writeXML(eval(CurrAllModelObj[0])));
	//var dataxml = formatXml(xotree.writeXML(eval(CurrModelObj[0])));
	$devModelData = dataxml;
	$.ajax({
		//url : '../orgMgr/devCfg/saveDevLogicCfg',
		url : '../orgMgr/devCfg/saveDevModelCfg?orgId=' + parent.CurrorgID,
		type : 'post',
		dataType : 'json',
		data : $devModelData,
		timeout : 100000,
		cache : false,
		error : function (XMLHttpRequest, textStatus, errorThrown) {
			alert("获取参数超时");
			return UpdatadevXmlStatus;
		},
		success : function (data) {
			if (data.resultCode != '10000') {
				bootbox.alert(data.resultMsg, function () {});
				return UpdatadevXmlStatus;
			} else {
				UpdataappXmlStatus = true;
				bootbox.alert(data.resultMsg, function () {
					return;
				});
				return UpdataappXmlStatus;
			}
		}
	})
}
/****加标签封装格式****/
function AppendModelData() {
	CurrAllModelObj = [];
	var name = "设备模板";
	if (EncapsulateModelData() == true) {
		CurrAllModelObj.push({
			"paramTypes" : {
				"@name" : name,
				"elemType" : CurElemTypeObjTemp
			}
		});
		return true;
	} else {
		alert("您还未配置任何参数，请配置后再保存");
		return false;
	}
}

/****CurElemTypeArray封装@****/
function EncapsulateModelData() {
	if (CurElemTypeArray.length != 0) {
		CurElemTypeObjTemp = [];
		var CurrModelObj = []; //model
		var CurCmdObj = [];
		var CurIObj = [];
		var iscmd = true;
		var isi = true;
		//if(typeof(CurElemTypeArray) == "object" && CurElemTypeArray.constructor == Array){//n  CurElemTypeArray
		CurElemTypeObj = [];
		$.each(CurElemTypeArray, function (indexetype, itemetype) {
			var etype = itemetype.etype;
			var name = itemetype.name;
			var newmodel = itemetype.model;
			if (typeof(newmodel) == "object" && newmodel.constructor == Array) { //n  model
				CurrModelObj = [];
				CurCmdObj = [];
				$.each(newmodel, function (indexmodel, itemmodel) {
					var modelid = itemmodel.id;
					var modelmodel = itemmodel.model;
					CurCmdObj = []; ////nice
					if (itemmodel.hasOwnProperty("cmd")) {
						iscmd = true;
						var newcmd = itemmodel.cmd;
						if (typeof(newcmd) == "object" && newcmd.constructor == Array) { //n  cmd
							iscmd = true;
							$.each(newcmd, function (indexcmd, itemcmd) {
								if (itemcmd.hasOwnProperty("i")) {
									isi = true;
									var newi = itemcmd.i;
									if (typeof(newi) == "object" && newi.constructor == Array) { // n  i
										isi = true;
										CurIObj = [];
										$.each(newi, function (indexi, itemi) {
											CurIObj.push({
												"@id" : itemi.id,
												"@oid" : itemi.oid,
												"@val" : itemi.val,
												"@dtype" : itemi.dtype,
												"@name" : itemi.name
											});
										});
										/////gai
										CurCmdObj.push({
											"@id" : itemcmd.id,
											"@ctype" : itemcmd.ctype,
											"@rw" : itemcmd.rw,
											"@modelName" : itemcmd.modelName,
											"i" : CurIObj
										});
									} else { // 1  i
										isi = true;
										CurIObj.push({
											"@id" : newi.id,
											"@oid" : newi.oid,
											"@val" : newi.val,
											"@dtype" : newi.dtype,
											"@name" : newi.name
										});
										CurCmdObj.push({
											"@id" : itemcmd.id,
											"@ctype" : itemcmd.ctype,
											"@rw" : itemcmd.rw,
											"@modelName" : itemcmd.modelName,
											"i" : CurIObj
										});
									}
								} else { // no  i
									CurCmdObj.push({
										"@id" : itemcmd.id,
										"@ctype" : itemcmd.ctype,
										"@rw" : itemcmd.rw,
										"@modelName" : itemcmd.modelName
									});
									isi = false;
								}
							});
						} else { // 1 cmd	    //newcmd
							if (newcmd == "0") {
								iscmd = false;
							} else {
								if (newcmd.hasOwnProperty("i")) {
									isi = true;
									var newi = newcmd.i;
									if (typeof(newi) == "object" && newi.constructor == Array) { // n  i
										isi = true;
										CurIObj = [];
										$.each(newi, function (indexi, itemi) {
											CurIObj.push({
												"@id" : itemi.id,
												"@oid" : itemi.oid,
												"@val" : itemi.val,
												"@dtype" : itemi.dtype,
												"@name" : itemi.name
											});
										});
									} else { //  1  i
										isi = true;
										CurIObj.push({
											"@id" : newi.id,
											"@oid" : newi.oid,
											"@val" : newi.val,
											"@dtype" : newi.dtype,
											"@name" : newi.name
										});
									}
								} else { // no i
									isi = false;
								}
								if (isi == false) {
									CurCmdObj.push({
										"@id" : newcmd.id,
										"@ctype" : newcmd.ctype,
										"@rw" : newcmd.rw,
										"@modelName" : newcmd.modelName
									});
								} else {
									CurCmdObj.push({
										"@id" : newcmd.id,
										"@ctype" : newcmd.ctype,
										"@rw" : newcmd.rw,
										"@modelName" : newcmd.modelName,
										"i" : CurIObj
									});
								}
							}
						}
					} else { //  no cmd
						iscmd = false;
					}
					if (iscmd == false) {
						CurrModelObj.push({
							"@id" : modelid,
							"@model" : modelmodel
						});
					} else {
						CurrModelObj.push({
							"@id" : modelid,
							"@model" : modelmodel,
							"cmd" : CurCmdObj
						});
					}
				});
			} else { // 1  model
				CurrModelObj = [];
				CurCmdObj = [];
				var modelid = newmodel.id;
				var modelmodel = newmodel.model;
				if (newmodel.hasOwnProperty("cmd")) {
					iscmd = true;
					var newcmd = newmodel.cmd;
					if (typeof(newcmd) == "object" && newcmd.constructor == Array) { //n  cmd
						CurCmdObj = [];
						iscmd = true;
						$.each(newcmd, function (indexcmd, itemcmd) {
							if (itemcmd.hasOwnProperty("i")) {
								isi = true;
								var newi = itemcmd.i;
								if (typeof(newi) == "object" && newi.constructor == Array) { // n  i
									isi = true;
									//CurCmdObj=[];
									CurIObj = [];
									$.each(newi, function (indexi, itemi) {
										CurIObj.push({
											"@id" : itemi.id,
											"@oid" : itemi.oid,
											"@val" : itemi.val,
											"@dtype" : itemi.dtype,
											"@name" : itemi.name
										});
									});
								} else { // 1  i
									isi = true;
									CurIObj.push({
										"@id" : newi.id,
										"@oid" : newi.oid,
										"@val" : newi.val,
										"@dtype" : newi.dtype,
										"@name" : newi.name
									});
								}
							} else { // no  i
								isi = false;
							}
							if (isi == false) {
								CurCmdObj.push({
									"@id" : itemcmd.id,
									"@ctype" : itemcmd.ctype,
									"@rw" : itemcmd.rw,
									"@modelName" : itemcmd.modelName
								});
							} else {
								CurCmdObj.push({
									"@id" : itemcmd.id,
									"@ctype" : itemcmd.ctype,
									"@rw" : itemcmd.rw,
									"@modelName" : itemcmd.modelName,
									"i" : CurIObj
								});
							}
						});
					} else { // 1 cmd	   //newcmd
						if (newcmd == "0") {
							iscmd = false;
						} else {
							if (newcmd.hasOwnProperty("i")) {
								isi = true;
								var newi = newcmd.i;
								if (typeof(newi) == "object" && newi.constructor == Array) { // n  i
									isi = true;
									CurIObj = [];
									CurCmdObj = [];
									$.each(newi, function (indexi, itemi) {
										CurIObj.push({
											"@id" : itemi.id,
											"@oid" : itemi.oid,
											"@val" : itemi.val,
											"@dtype" : itemi.dtype,
											"@name" : itemi.name
										});
									});
								} else { //  1  i
									isi = true;
									CurIObj.push({
										"@id" : newi.id,
										"@oid" : newi.oid,
										"@val" : newi.val,
										"@dtype" : newi.dtype,
										"@name" : newi.name
									});
								}
							} else { // no i
								//CurCmdObj.push({"@id":newcmd.id,"@ctype": newcmd.ctype,"@rw": newcmd.rw,"@modelName": newcmd.modelName});
								isi = false;
							}
							if (isi == false) {
								CurCmdObj.push({
									"@id" : newcmd.id,
									"@ctype" : newcmd.ctype,
									"@rw" : newcmd.rw,
									"@modelName" : newcmd.modelName
								});
							} else {
								CurCmdObj.push({
									"@id" : newcmd.id,
									"@ctype" : newcmd.ctype,
									"@rw" : newcmd.rw,
									"@modelName" : newcmd.modelName,
									"i" : CurIObj
								});
							}
						}
					}
				} else { //  no cmd
					iscmd = false;
				}
				if (iscmd == false)
					CurrModelObj.push({
						"@id" : modelid,
						"@model" : modelmodel
					});
				else
					CurrModelObj.push({
						"@id" : modelid,
						"@model" : modelmodel,
						"cmd" : CurCmdObj
					});
			}
			////1 model end
			CurElemTypeObj.push({
				"@etype" : etype,
				"@name" : name,
				"model" : CurrModelObj
			});
		});
		CurElemTypeObjTemp = CurElemTypeObj;
		return true;
		//}
		//1 CurElemTypeObj   start
		//1 CurElemTypeObj   end
	}
	return true;
}